package com.baibei.pay.recharge;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.baibei.pay.R;
import com.baibei.pay.R2;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

public class AlipayWebviewActivity extends AppCompatActivity {

    @BindView(R2.id.wv)
    WebView wv;
    private boolean isOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay_webview);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String codeUrl = intent.getStringExtra("codeUrl");
//        wv.getSettings().setJavaScriptEnabled(true);
//        Map<String, String> extraHeaders = new HashMap<>();
//        extraHeaders.put("Referer", "http://www.100bei.com");
//        wv.loadUrl(codeUrl,extraHeaders);


//        wv.getSettings().setJavaScriptEnabled(true);
//        wv.setWebViewClient(new WebViewClient(){
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (url.startsWith("https://wx.tenpay.com")) {
//                    Map<String, String> extraHeaders = new HashMap<>();
//                    extraHeaders.put("Referer", "http://www.100bei.com");
//                    view.loadUrl(url, extraHeaders);
//                    return true;
//                }
//                view.loadUrl(url);
//                return true;
//            }
//        });
//        wv.loadUrl(codeUrl,extraHeaders);
//        wv.loadUrl(codeUrl);

        Map<String, String> map = new HashMap<>();
        map.put("Referer","http://www.100bei.com");
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wv.setWebChromeClient(new WebChromeClient());
        WebViewClient webViewClient = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 如下方案可在非微信内部WebView的H5页面中调出微信支付
                if (url.startsWith("weixin://wap/pay?")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    return true;
                } else {
                    Map<String, String> extraHeaders = new HashMap<String, String>();
                    extraHeaders.put("Referer", "http://wxpay.wxutil.com");
                    view.loadUrl(url, extraHeaders);
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) { // 重写此方法可以让webview处理https请求
                handler.proceed();
            }
        };
        wv.setWebViewClient(webViewClient);
        wv.loadUrl(codeUrl, map);
    }
}

